package com.yhf.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yhf.dto.ContractorDto;
import com.yhf.dto.DeveloperDto;
import com.yhf.model.Contractor;
import com.yhf.model.Developer;
import com.yhf.model.Project;
import com.yhf.service.ContractorService;
import com.yhf.service.PService;
import com.yhf.utils.PageResult;
import com.yhf.utils.QueryPageBean;
import com.yhf.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("contractor")
public class ContractorController {
    @Autowired
    ContractorService contractorService;
    @Autowired
    PService pService;
    @RequestMapping("all")
    public PageResult all(@RequestBody QueryPageBean queryPageBean){
        QueryWrapper<Contractor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",1);
        System.out.println(queryWrapper.getSqlSelect());
        Page<Contractor> page = new Page<>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Contractor> page1 = contractorService.page(page,queryWrapper);
        PageResult result = new PageResult();
        result.setRows(page1.getRecords());
        result.setTotal(page1.getTotal());
        return result;
    }
    @RequestMapping("allproject")
    public Result allproject(){
        Result result = new Result();
        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",1);
        List<Project> list = pService.list(queryWrapper);
        if (list != null) {
            result.setData(list);
            result.setFlag(true);
            result.setMessage("success");
            return result;
        }else {
            return Result.fail();
        }

    }
    @RequestMapping("add")
    public Result addDeveloper(String cname,int [] pids){
        System.out.println(cname);
        System.out.println(Arrays.toString(pids));
        Contractor developer = new Contractor();
        developer.setCname(cname);
        developer.setStatus(1);
        boolean save = contractorService.save(developer);

        Project project = new Project();
        for (int pid : pids) {
            QueryWrapper queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("pid",pid);
            project.setCid(developer.getCid());
            pService.update(project, queryWrapper);
        }

        Result result  = new Result();
        if (save){
            result.setMessage("success");
            result.setFlag(true);
        }else {
            result.setMessage("fail");
        }
        return result;
    }

    @GetMapping("findpjandcnameById")
    public Result findBydnId(@RequestParam String cid){
        QueryWrapper<Contractor> queryWrapper = new QueryWrapper<>();
        QueryWrapper<Project> queryWrapper1 = new QueryWrapper<>();
        queryWrapper.eq("cid",cid);
        queryWrapper1.eq("cid",cid);
        Contractor one = contractorService.getOne(queryWrapper);
        List<Project> list = pService.list(queryWrapper1);
        ContractorDto developerDto = new ContractorDto();
        int[] arr = new int[list.size()];
        for (int i = 0;i<list.size();i++){
            arr[i] = list.get(i).getPid();
        }
        developerDto.setPids(arr);
        developerDto.setCid(one.getCid());
        developerDto.setCname(one.getCname());
        Result result = new Result();
        result.setData(developerDto);
        result.setFlag(true);
        result.setMessage("success");
        return result;
    }
    @PostMapping("edit")
    public Result edit(@RequestBody ContractorDto developerDto){
        Contractor contractor = new Contractor();
        contractor.setCname(developerDto.getCname());
        contractor.setUpdatetime(new Date());
        contractor.setStatus(1);
        QueryWrapper<Contractor> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.eq("cid",developerDto.getCid());
        //修改承建商名称和修改时间
        contractorService.update(contractor,queryWrapper3);
        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cid",developerDto.getCid());
        //用来清空之前的cid
        Project project = new Project();
        project.setCid("");
        //清空之前的cid
        pService.update(project,queryWrapper);
        Project project1 = new Project();
        project1.setCid(developerDto.getCid());


        for (int i = 0; i < developerDto.getPids().length; i++) {
            QueryWrapper<Project> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("pid",developerDto.getPids()[i]);
            pService.update(project1,queryWrapper1);
        }
        return Result.success();
    }
    @GetMapping("delete")
    public Result delete(@RequestParam String cid){
        QueryWrapper<Contractor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cid",cid);
        Contractor developer = new Contractor();
        developer.setStatus(0);
        contractorService.update(developer,queryWrapper);
        QueryWrapper<Project> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("cid",cid);
        Project project = new Project();
        project.setCid("");
        pService.update(project,queryWrapper1);
        return Result.success();

    }
}
